package com.softaria.spkiller.layers.classificator;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.softaria.spkiller.layers.LayerIdentifier;
import com.softaria.spkiller.metadata.ClassIdentifier;
import com.softaria.spkiller.metadata.ClassMetaData;

public class LayerManager implements Classificator{
	
	private Map<LayerIdentifier,Collection<ClassIdentifier>> classesByLayers = new HashMap<LayerIdentifier, Collection<ClassIdentifier>>();
	private ClassificatorCacheWrapper classifictor;

	private LayerManager(Collection<ClassIdentifier> allClasses,ClassificatorCacheWrapper classificator) {
		
		this.classifictor = classificator;
		
		for(ClassIdentifier classID:allClasses) {
			LayerIdentifier layer = classificator.classificate(classID);
			
			put(layer,classID);
		}
	}
	
	public static LayerManager createLayerManager(Collection<ClassIdentifier> allClasses,Classificator classificator) {
		return new LayerManager(allClasses,new ClassificatorCacheWrapper(classificator));
	}

	private void put(LayerIdentifier layer, ClassIdentifier classID) {
		Collection<ClassIdentifier> classes =  classesByLayers.get(layer);
		
		if(classes==null) {
			classes = new HashSet<ClassIdentifier>();
			classesByLayers.put(layer,classes);
		}
		
		classes.add(classID);
		
	}
	
	public Collection<ClassIdentifier> getAllClasses() {
		return classifictor.getAllClasses();
	}
	
	public Collection<ClassIdentifier> getLayerClasses(LayerIdentifier layer) {
		return classesByLayers.get(layer);
	}

	@Override
	public LayerIdentifier classificate(ClassIdentifier classID) {
		return classifictor.classificate(classID);
	}
	
	public Collection<LayerIdentifier> getAllLayers() {
		return classifictor.getAllLayers();
	}

	@Override
	public LayerIdentifier classificate(ClassMetaData metaData) {
		return classifictor.classificate(metaData);
	}
	
	
}
